<?php

namespace App\Http\Controllers\Api\Train;

use App\Http\Controllers\Controller;
use App\Trip\Train\Providers\PiaoChi\Callback\OrderNotify;
use App\Trip\Train\Providers\PiaoChi\Callback\ValidNotifyData;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CallbackController extends Controller
{
    public function callback($source, Request $request)
    {
        $notify = $request->all();
        Log::info(json_encode(compact('source', 'notify'), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        switch ($source) {
            case 'piaochi':
                $valid = new  ValidNotifyData($notify);
                try {
                    $process = new OrderNotify($valid);
                    $process->ProcessNotify();
                } catch (Exception $exception) {
                    Log::error("piaochi:callback:{$exception->getMessage()}");
                }
                return response($valid->getResponse());
            default:
                return $this->error(1, '', [], 404);
        }
    }
}
